import {app, Menu, MenuItemConstructorOptions, shell} from 'electron'
import windowManager from './windowManager'

const menuTemplates: MenuItemConstructorOptions[] = [{
  label: '文件',
  submenu: [{
    label: '新链接窗口',
    accelerator: 'CmdOrCtrl+N',
    click() {
      windowManager.create()
    }
  }, {
    label: '新链接标签',
    accelerator: 'CmdOrCtrl+T',
    click() {
      windowManager.current.webContents.send('action', 'createInstance')
    }
  }, {
    type: 'separator'
  }, {
    label: '关闭窗口',
    accelerator: 'Shift+CmdOrCtrl+W',
    click() {
      windowManager.current.close()
    }
  }, {
    label: '关闭标签',
    accelerator: 'CmdOrCtrl+W',
    click() {
      windowManager.current.webContents.send('action', 'delInstance')
    }
  }]
}, {
  label: '编辑',
  submenu: [{
    label: '撤销',
    accelerator: 'CmdOrCtrl+Z',
    role: 'undo'
  }, {
    label: '重做',
    accelerator: 'Shift+CmdOrCtrl+Z',
    role: 'redo'
  }, {
    type: 'separator'
  }, {
    label: '剪切',
    accelerator: 'CmdOrCtrl+X',
    role: 'cut'
  }, {
    label: '拷贝',
    accelerator: 'CmdOrCtrl+C',
    role: 'copy'
  }, {
    label: '粘贴',
    accelerator: 'CmdOrCtrl+V',
    role: 'paste'
  }, {
    label: '选择全部',
    accelerator: 'CmdOrCtrl+A',
    role: 'selectall'
  }]
}, {
  label: '视图',
  submenu: [{
    label: '重新加载',
    accelerator: 'CmdOrCtrl+R',
    click(item, focusedWindow) {
      if (focusedWindow) {
        focusedWindow.reload()
      }
    }
  }, {
    label: '切换全屏',
    accelerator: (function () {
      if (process.platform === 'darwin') {
        return 'Ctrl+Command+F'
      }
      return 'F11'
    })(),
    click(item, focusedWindow) {
      if (focusedWindow) {
        focusedWindow.setFullScreen(!focusedWindow.isFullScreen())
      }
    }
  }, {
    label: '切换开发者工具',
    accelerator: (function () {
      if (process.platform === 'darwin') {
        return 'Alt+Command+I'
      }
      return 'Ctrl+Shift+I'
    })(),
    click(item, focusedWindow) {
      if (focusedWindow) {
        focusedWindow.webContents.toggleDevTools()
      }
    }
  }]
}, {
  label: '窗口',
  role: 'window',
  submenu: [{
    label: '最小化',
    accelerator: 'CmdOrCtrl+M',
    role: 'minimize'
  }, {
    label: '关闭',
    accelerator: 'CmdOrCtrl+W',
    role: 'close'
  }]
}, {
  label: '帮助',
  role: 'help',
  submenu: [{
    label: '联系我们...',
    click() {
      shell.openExternal('https://dev.tekin.cn/contactus.html')
    }
  }, {
    label: '软件定制开发',
    click() {
       shell.openExternal('https://dev.tekin.cn')
    }
  }]
}]

let baseIndex = 0
if (process.platform == 'darwin') {
  baseIndex = 1
  menuTemplates.unshift({
    label: app.getName(),
    submenu: [{
      label: '关于 ' + app.getName() + ' Tekin编译定制版',
      role: 'about'
    }, {
      type: 'separator'
    }, {
      label: '服务',
      role: 'services',
      submenu: []
    }, {
      type: 'separator'
    }, {
      label: '隐藏 ' + app.getName(),
      accelerator: 'Command+H',
      role: 'hide'
    }, {
      label: '隐藏其他',
      accelerator: 'Command+Shift+H',
      role: 'hideothers'
    }, {
      label: '显示所有',
      role: 'unhide'
    }, {
      type: 'separator'
    }, {
      label: '退出',
      accelerator: 'Command+Q',
      click() {
        app.quit()
      }
    }]
  })
}

const menu = Menu.buildFromTemplate(menuTemplates)

if (process.env.NODE_ENV === 'production') {
  const {submenu} = (menu.items[baseIndex + 2] as any)
  submenu.items[0].visible = false
  submenu.items[2].visible = false
}

const {submenu} = (menu.items[baseIndex + 0] as any)
windowManager.on('blur', function () {
  submenu.items[3].enabled = false
  submenu.items[4].enabled = false
})

windowManager.on('focus', function () {
  const {submenu} = (menu.items[baseIndex + 0] as any)
  submenu.items[3].enabled = true
  submenu.items[4].enabled = true
})

export default menu
